{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4278f0a7-30cf-422f-b4aa-2bdd90db7299",
   "metadata": {},
   "source": [
    "# 单电机实验 demo"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72147c7a-2d49-49fb-87b7-f40b02bf1b74",
   "metadata": {},
   "source": [
    "## 初始化电机"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "a6ae7b4f-9d6d-4e63-ac80-0dd6a92fd22b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from protobot.can_bus import Robot\n",
    "from protobot.can_bus.nodes import MotorFactory\n",
    "robot = Robot()\n",
    "motor_id = 0x10\n",
    "reduction = -12.45\n",
    "motor = robot.add_device('motor0', MotorFactory(), motor_id, reduction=reduction)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abaf865c-2c3a-4c5a-ba4c-08de67f14ba3",
   "metadata": {},
   "source": [
    "## 使能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "600e1068-b22a-4d4f-ac7e-1893d5ff46c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "motor.enable()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64d592f6-717a-412b-8332-685cd039a9af",
   "metadata": {},
   "source": [
    "## 位置模式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ff67c1e5-08e7-4516-ac7c-ed9fb66e62c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f981a75bf67c4d129cbd376f47571705",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(Dropdown(description='mode', options=('直接', '滤波', '匀加减速'), value='直接'), FloatSlider(valu…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from ipywidgets import interact\n",
    "@interact(mode = ['直接', '滤波', '匀加减速'], pos=(-3.14, 3.14, 0.01))\n",
    "def demo(mode, pos = 0):\n",
    "    if mode == '直接':\n",
    "        motor.position_mode()\n",
    "    elif mode == '滤波':\n",
    "        motor.position_filter_mode(4)\n",
    "    elif mode == '匀加减速':\n",
    "        motor.position_traj_mode(2,0.5,0.5)\n",
    "    motor.set_pos(pos)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7bef62e0-a81e-4d1e-878a-9f676d6f5e87",
   "metadata": {},
   "source": [
    "## 速度模式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "86b153fb-c98e-4e7a-b7db-1abf31f4ec54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "3cafc867b33a4a789586f7de5d4d0cbf",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(Dropdown(description='mode', options=('直接', '匀加减速'), value='直接'), FloatSlider(value=0.0,…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from ipywidgets import interact\n",
    "@interact(mode = ['直接', '匀加减速'], vel=(-3.14, 3.14, 0.01))\n",
    "def demo(mode, vel = 0):\n",
    "    if mode == '直接':\n",
    "        motor.velocity_mode()\n",
    "    elif mode == '匀加减速':\n",
    "        motor.velocity_ramp_mode(0.5)\n",
    "    motor.set_vel(vel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a251014-72bc-42fa-88ca-290c739720dc",
   "metadata": {},
   "source": [
    "## 力矩模式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "55dff783-fa69-4523-9c1d-75bf829d80b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a066fb55ece04e9fa2aa67f68b599a65",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(FloatSlider(value=0.0, description='torque', max=3.0, min=-3.0, step=0.01), Output()), _…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from ipywidgets import interact\n",
    "@interact(torque=(-3, 3, 0.01))\n",
    "def demo(torque = 0):\n",
    "    motor.torque_mode()\n",
    "    motor.set_torque(torque/100.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f615a5b-3220-4be0-98aa-c8daa825f835",
   "metadata": {},
   "source": [
    "## 读取电机数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "feed390c-ebe7-49b9-a891-fb0cf397fd98",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "位置: 4.6608729847758665\n",
      "速度: -0.0\n",
      "力矩: -0.1369697570800781\n"
     ]
    }
   ],
   "source": [
    "print('位置: {}'.format(motor.get_pos()))\n",
    "print('速度: {}'.format(motor.get_vel()))\n",
    "print('力矩: {}'.format(motor.get_torque()))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdb55080-fa8a-49d8-bd52-cd9f5a6f2466",
   "metadata": {},
   "source": [
    "## 失能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "241d1a45-98a5-47f1-b732-30e645a3da2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "motor.disable()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2eee1033-526b-4541-bf2f-472329ffe242",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
